<?php


namespace app\admin\controller;
use app\common\lib\Auth;
use think\Db;
use think\Request;

class Top extends Base
{
    public function top_menu()
    {
        if(request()->isAjax()){
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            try {
                if (isset($keyword) && $keyword != "") {
                    $count = Db::name('index_menu')->where($map)->count();  //总条目数据
                    $lists = Model('IndexMenu')->ajax_tree($NowPage, $limits, 'title', $keyword);  //当前页
                }else {
                    $count = Db::name('index_menu')->count();  //总条目数据
                    $lists = Model('IndexMenu')->ajax_tree($NowPage, $limits, 'title', "");  //当前页
                }
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }else {
            /*************************检测权限 开始***********************/
            $auth = new Auth();
            $request = Request::instance();
            if(!$auth->check($request->path(),$this->isLogin()->id)) {
                return $this->redirect('not_rule/index/index');
            }
            /*************************检测权限 结束***********************/
            return $this->fetch();
        }
    }

    public function top_menu_add()
    {
        if(request()->isAjax()){
            $data = input('post.');
            $Plevel = Model('IndexMenu')->getPlevel($data['pid']);
            if($Plevel) {
                $data['level'] = $Plevel['0'] + 1;
            }else{
                $data['level'] = 1;
            }
            try{
                $_add_res = Model('IndexMenu')->save($data);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','添加成功，刷新父页面生效','1');
        }else {
            $index_menu = Model('IndexMenu')->getFirstNav();
            $this->assign([
                'index_menu' => $index_menu,
            ]);
            return $this->fetch();
        }
    }

    public function top_menu_edit()
    {
        if(request()->isAjax()) {
            $data = input('post.');
            $Plevel = Model('IndexMenu')->getPlevel($data['pid']);
            if($Plevel) {
                $data['level'] = $Plevel['0'] + 1;
            }else{
                $data['level'] = 1;
            }
//            return show('200','ok',$data);
            try{
                $_add_res = Model('IndexMenu')->save($data,['id'=>$data['id']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','修改成功，刷新父页面生效','1');
        }else{
            $id = Request::instance()->param('id');
            $this_data = Model('IndexMenu')->get(['id'=>$id]);
            $this->assign([
                'this_data' => $this_data
            ]);
            $this->top_menu_add();
            return $this->fetch();
        }
    }

    public function top_menu_del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('id');
            try{
                $res = Model('IndexMenu')->del_msg($id);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            return show('200','删除成功！','1');
        }else{
            return show('500','非法操作');
        }
    }

    public function top_logo()
    {
        $_img = $this->_config->get_this_config_value('4', 'logo');
        $this->assign([
            'img' => $_img
        ]);

        /*************************检测权限 开始***********************/
        $auth = new Auth();
        $request = Request::instance();
        if(!$auth->check($request->path(),$this->isLogin()->id)) {
            return $this->redirect('not_rule/index/index');
        }
        /*************************检测权限 结束***********************/
        return $this->fetch();
    }

    public function top_banner()
    {
        if(request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            if(isset($keyword) && $keyword!="")
            {
                $map['b_name'] = ['like',"%" . $keyword . "%"];
            }
            try {
                $count = Db::name('banner')->count();  //总条目数据
                $lists = Db::name('banner')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        /*************************检测权限 开始***********************/
        $auth = new Auth();
        $request = Request::instance();
        if(!$auth->check($request->path(),$this->isLogin()->id)) {
            return $this->redirect('not_rule/index/index');
        }
        /*************************检测权限 结束***********************/
        return $this->fetch();
    }

    public function top_banner_add()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $_has = Model('Banner')->get(['b_name'=>$data['b_name']]);
            if($_has) {
                return show('200','该类目已存在，禁止重复添加','0');
            }
            try{
                $_add_res = Model('Banner')->allowField(true)->save($data);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            return show('200','添加成功，刷新父页面生效','1');
        }else{
            return $this->fetch();
        }
    }

    public function top_banner_edit()
    {
        if (\request()->isAjax()){
            $data = input('post.');
            try{
                $_edit_res = Model('Banner')->allowField(true)->save($data,['id'=>$data['id']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_edit_res) {
                return show('200','数据库错误','0');
            }
            return show('200','修改成功，刷新父页面生效','1');
        }else {
            $id = Request::instance()->param('id');
            $this_data = Model('Banner')->get(['id' => $id]);
            $this->assign([
                'this_data' => $this_data
            ]);
            return $this->fetch();
        }
    }

    public function top_banner_del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('id');
            try{
                $res = Model('Banner')->del_msg($id);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            return show('200','删除成功！','1');
        }else{
            return show('500','非法操作');
        }
    }
}